-
Notifications
You must be signed in to change notification settings - Fork 15
/
24 - Swaps Nodes in Pairs.py
59 lines (46 loc) · 980 Bytes
/
24 - Swaps Nodes in Pairs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Node:
def __init__(self, data=0):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def push(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
curr = self.head
while curr.next:
curr = curr.next
curr.next = new_node
def disp(self):
if self.head is None:
print("Empty Nodes")
curr = self.head
while curr:
print(curr.data, end = ' ')
curr = curr.next
print()
def swaps(self):
tail = prev = Node()
tail.next = self.head
while tail.next and tail.next.next:
first = tail.next
second = tail.next.next
third = tail.next.next.next
tail.next = second
tail.next.next = first
tail.next.next.next = third
tail = first
return prev.next
ll = LinkedList()
arr = [1, 2, 3, 4]
for i in arr:
ll.push(i)
# new_val = LinkedList()
ll.disp()
new_val = ll.swaps()
while new_val:
print(new_val.data, end=' ')
new_val = new_val.next